package com.prp.struts.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.prp.dao.Comment;
import com.prp.dao.Editlog;
import com.prp.dao.Grade;
import com.prp.dao.Item;
import com.prp.dao.Userinfo;
import com.prp.service.ServiceFactory;

public class CommentAction extends ActionSupport {
	
	
	private Set commentList;
	private int itemID;
	private String username;
	private Comment comm;
	private Set collectList;
	private Item item;
	private String isGrade;
	private String uername;
	private Editlog uploadInfo;
	
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public int getGradeNum() {
		return gradeNum;
	}
	public void setGradeNum(int gradeNum) {
		this.gradeNum = gradeNum;
	}
	public double getGrade() {
		return grade;
	}
	public void setGrade(double grade) {
		this.grade = grade;
	}
	private int gradeNum;
	private double grade;
	
	public void setCommentList(Set commentList) {
		this.commentList = commentList;
	}
	public Set getCommentList() {
		return commentList;
	}
	public void setItemID(int itemID) {
		this.itemID = itemID;
	}
	public int getItemID() {
		return itemID;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getUsername() {
		return username;
	}
	
	public String getDcoInfo(){
		username = (String) ActionContext.getContext().getSession().get("username");
		
		item = ServiceFactory.getFileManage().getFileInfo(itemID);
		this.gradeNum = item.getGrades().size();
		
		this.grade = ServiceFactory.getGradeService().getAvgGrade(item);
		
		this.isGrade = ServiceFactory.getGradeService().isGrade(username, item).toString();
		
		this.uploadInfo = ServiceFactory.getFileInfoService().getUploadInfo(itemID);
		
		return SUCCESS;
	}
	public String showMyCollect(){
		String username = (String) ActionContext.getContext().getSession().get("username");
		Userinfo user = ServiceFactory.getAdminSystemSer().getUser(username);
		this.collectList = user.getCollects();
		
		return SUCCESS;
	}
	
	public String delCollectInUser(){
		String username = (String) ActionContext.getContext().getSession().get("username");
		ServiceFactory.getCollectService().delCollect(username, itemID);
		
		
		return SUCCESS;
	}
	
	public String commitComm(){
		String user = (String) ActionContext.getContext().getSession().get("username");
		if (user == null)
			return INPUT;
		ServiceFactory.getCommentService().commitComm(comm, user, itemID);
		return SUCCESS;
	}
	
	
	public String showComment(){
		commentList = ServiceFactory.getCommentService().getCommentList(itemID);
		
		return SUCCESS;
	}
	
	
	public String collectItem() throws IOException{
		String user = (String) ActionContext.getContext().getSession().get("username");
		
		
		HashMap map = new HashMap();
		map.put("success", "fail");
		if (ServiceFactory.getCollectService().collectItem(user, itemID) != -1)
			map.put("success", "success");
		
		
		printReslut(map);
		
		
		return null;
	}
	public String delCollect() throws IOException{
		String user = (String) ActionContext.getContext().getSession().get("username");
		
		
		HashMap map = new HashMap();
		map.put("success", "fail");
		if (ServiceFactory.getCollectService().delCollect(user, itemID) != -1)
			map.put("success", "success");
		
		
		printReslut(map);				
		return null;
		
	}
	public String gradeItem() throws IOException{
		String user = (String) ActionContext.getContext().getSession().get("username");
		
		
		HashMap map = new HashMap();

		ServiceFactory.getGradeService().gradeItem(user, itemID, (int)grade);
		
			
		item = ServiceFactory.getFileManage().getFileInfo(itemID);
		this.gradeNum = item.getGrades().size();		
		this.grade = ServiceFactory.getGradeService().getAvgGrade(item);
		map.put("grade", grade);
		
		
		map.put("gradeNum", gradeNum);
		printReslut(map);
		
		return null;
		
	}
	public String delComment(){
		String user = (String) ActionContext.getContext().getSession().get("username");
		
		return null;
	}
	
	private void printReslut(Map map) throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();		
		response.setCharacterEncoding("UTF-8");    		       
		PrintWriter out = response.getWriter();										
		JSONObject json = JSONObject.fromObject(map);
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
	public void setComm(Comment comm) {
		this.comm = comm;
	}
	public Comment getComm() {
		return comm;
	}
	public void setCollectList(Set collectList) {
		this.collectList = collectList;
	}
	public Set getCollectList() {
		return collectList;
	}
	public void setIsGrade(String isGrade) {
		this.isGrade = isGrade;
	}
	public String getIsGrade() {
		return isGrade;
	}
	public void setUername(String uername) {
		this.uername = uername;
	}
	public String getUername() {
		return uername;
	}
	public void setUploadInfo(Editlog uploadInfo) {
		this.uploadInfo = uploadInfo;
	}
	public Editlog getUploadInfo() {
		return uploadInfo;
	}

}
